﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Telerik.Web.UI;
using System.Data;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.Exam
{
    public partial class BmManage : BasePage
    {
        #region Params

        private string CurPageCode = "BM";

        private string CurPage = "BmManage.aspx";

        private string QCPageCode = "QC";

        private string QCPage = "BmQCList.aspx";

        public string AppID
        {
            get
            {
                return HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString();
            }
        }

        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }

        /// <summary>
        /// 作为查询地区限定条件
        /// </summary>
        protected string AreaCodeWhere
        {
            get
            {
                return UserInfo.LocalArea;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindQCDDL();
                //绑定列表
                rgPersonList.DataSource = ToDataTable();
            }
        }

        #region 下拉列表操作

        protected void ddlQC_SelectedIndexChanged(object sender, EventArgs e)
        {
            rgPersonList.DataSource = ToDataTable();
            rgPersonList.DataBind();
        }
        
        private void BindQCDDL()
        {
            using (PersonServiceClient client = new PersonServiceClient())
            {
                int count = 0;
                var qc = JsonConvert.DeserializeObject<List<Exam_QC>>(client.GetExamQCList
                       (QCPageCode, UserInfo.RoleId, QCPage, AppID, 0, 0, out count));
                ddlQC.DataSource = qc;
                ddlQC.DataValueField = "ID";
                ddlQC.DataTextField = "Title";
                ddlQC.DataBind();
                if (qc.Count == 0)
                {
                    ddlQC.Items.Insert(0, new ListItem("--请选择--", "0"));
                }
            }
        }

        #endregion

        #region 数据绑定

        /// <summary>
        /// 格式化结果集
        /// </summary>
        protected void rgPersonList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                ///隐藏地区号
                GridHeaderItem gridHearItem = e.Item as GridHeaderItem;
                gridHearItem["DBType"].Visible = false;
            }

            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                gridItem["DBType"].Visible = false;
                SetTargetUrl(gridItem, "应报名人数", gridItem["DBType"].Text);
                SetTargetUrl(gridItem, "已报名缴费人数", gridItem["DBType"].Text);
                SetTargetUrl(gridItem, "未报名缴费人数", gridItem["DBType"].Text);
            }
        }

        /// <summary>
        /// 设置二级页面url连接
        /// </summary>
        /// <param name="dbType">地区</param>
        /// <param name="count">数量</param>
        private void SetTargetUrl(GridDataItem gItem,string columnName,string dbType)
        {
            int count = 0;
            string countStr = gItem[columnName].Text;
            string dbTypeText = gItem["地区"].Text;
            string url = String.Empty;
            
            int.TryParse(countStr, out count);
            
            if (count > 0)
            {
                ////二级页面的连接参数中 type: 0表示已报名，1表示未报名
                switch (columnName)
                {
                    case "应报名人数":
                        url = countStr;
                        break;
                    case "已报名缴费人数":
                        url = String.Format("<a href='BmList.aspx?type=0&areaCode={0}&qc={1}&title={2}已报名专家汇总'><span style='color: #43a9f5'>{3}</span></a>"
                            , dbType, ddlQC.SelectedValue.Trim(), dbTypeText, countStr);
                        break;
                    case "未报名缴费人数":
                        url = String.Format("<a href='BmList.aspx?type=1&areaCode={0}&qc={1}&title={2}未报名专家汇总'><span style='color: #43a9f5'>{3}</span></a>"
                            , dbType, ddlQC.SelectedValue.Trim(), dbTypeText, countStr);
                        break;
                    default: break;
                }
                gItem[columnName].Text = url;
            }
        }

        /// <summary>
        /// 取得数据源并将数据源转化成DataTable
        /// Table的列为 DBType,地区，应报名人数，已报名缴费人数，未报名缴费人数
        /// </summary>
        public DataTable ToDataTable()
        {
            int qcId=0;
            int.TryParse(ddlQC.SelectedValue.Trim(),out qcId);
            using (PersonServiceClient client = new PersonServiceClient())
            {
                //取得统计数据结果集
                var statist = JsonConvert.DeserializeObject<StatisticsStruct>(client.GetExpertBMStatistic(qcId, AreaCodeWhere, AreaCode));
                //转化成DataTable
                DataTable result = UtilitiesTool.StructToDataTable(statist);

                return result;
            }
        }

        #endregion
    }
}